<template>
  <div class="search-box">
    <el-input
      v-model="localSearchText"
      :placeholder="placeholder"
      :size="size"
      clearable
      class="search-input"
      @keyup.enter="handleSearch"
      @clear="handleClear"
      @focus="showHistory = true"
      @blur="handleBlur"
    >
      <template #append>
        <el-button 
          :type="buttonType" 
          :size="size" 
          @click="handleSearch"
          :loading="loading"
        >
          {{ buttonText }}
        </el-button>
      </template>
    </el-input>
    
    <!-- 搜索历史下拉 -->
    <div v-if="showHistory && searchHistory.length > 0" class="search-history-dropdown">
      <div class="history-header">
        <span class="history-title">搜索历史</span>
        <el-button 
          text 
          type="danger" 
          size="small" 
          @click.stop="handleClearHistory"
        >
          清空
        </el-button>
      </div>
      <div class="history-items">
        <div
          v-for="(item, index) in searchHistory"
          :key="index"
          class="history-item"
          @click.stop="handleHistoryClick(item)"
        >
          <el-icon><Clock /></el-icon>
          <span>{{ item }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Clock } from '@element-plus/icons-vue'

const props = defineProps({
  modelValue: {
    type: String,
    default: ''
  },
  placeholder: {
    type: String,
    default: '搜索'
  },
  size: {
    type: String,
    default: 'large'
  },
  buttonText: {
    type: String,
    default: '搜索'
  },
  buttonType: {
    type: String,
    default: 'primary'
  },
  loading: {
    type: Boolean,
    default: false
  },
  searchHistory: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['update:modelValue', 'search', 'clear', 'clearHistory', 'historyClick'])

const localSearchText = ref(props.modelValue)
const showHistory = ref(false)

watch(() => props.modelValue, (newVal) => {
  localSearchText.value = newVal
})

watch(localSearchText, (newVal) => {
  emit('update:modelValue', newVal)
})

const handleSearch = () => {
  if (!localSearchText.value.trim()) {
    return
  }
  showHistory.value = false
  emit('search', localSearchText.value)
}

const handleClear = () => {
  emit('clear')
  showHistory.value = false
}

const handleBlur = () => {
  // 延迟关闭，以便点击历史记录
  setTimeout(() => {
    showHistory.value = false
  }, 200)
}

const handleClearHistory = () => {
  emit('clearHistory')
  showHistory.value = false
}

const handleHistoryClick = (keyword) => {
  localSearchText.value = keyword
  showHistory.value = false
  emit('historyClick', keyword)
}
</script>

<style scoped>
.search-box {
  position: relative;
  width: 100%;
}

.search-input :deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #e5e5e5 inset;
}

.search-history-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  background: #fff;
  border: 1px solid #e5e5e5;
  border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  max-height: 300px;
  overflow-y: auto;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.history-title {
  font-size: 14px;
  color: #666;
  font-weight: 600;
}

.history-items {
  padding: 8px 0;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  cursor: pointer;
  transition: background 0.2s;
  color: #666;
  font-size: 14px;
}

.history-item:hover {
  background: #f5f5f5;
  color: #00c293;
}

.history-item .el-icon {
  font-size: 16px;
  color: #999;
}
</style>


